package BinaryTree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class BinaryTree {

    static class TreeNode {
        public char val;

        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    //创造一棵树的的结构
    public TreeNode createTree() {
        TreeNode node1=new TreeNode('A');
        TreeNode node2=new TreeNode('B');
        TreeNode node3=new TreeNode('C');
        TreeNode node4=new TreeNode('D');
        TreeNode node5=new TreeNode('E');
        TreeNode node6=new TreeNode('F');

        node1.left=node2;
        node1.right=node3;
        node2.left=node4;
        node2.right=node5;
        node3.left=node6;
        return node1;

    }
    // 前序遍历
    public void preOrder(TreeNode root) {

        if(root==null){
            return ;
        }
        System.out.println(root.val);
        preOrder(root.left);
        preOrder(root.right);
    }

    // 中序遍历
    void inOrder(TreeNode root) {

        if(root==null){
            return;
        }
        inOrder(root.right);
        System.out.println(root.val);
        inOrder(root.right);
    }

    // 后序遍历
    void postOrder(TreeNode root) {
        if(root==null){
            return;
        }
        inOrder(root.right);
        inOrder(root.right);
        System.out.println(root.val);
    }


    public static int nodeSize;

    /**
     * 获取树中节点的个数：遍历思路
     */
    void size(TreeNode root) {
        if (root==null){
            return;
        }
        nodeSize++;
        inOrder(root.right);
        inOrder(root.right);
    }

    /**
     * 获取节点的个数：子问题的思路
     *
     * @param root
     * @return
     */
    int size2(TreeNode root) {
        if(root==null){
            return 0;
        }

        int si1=size2(root.left);
        int si2=size2(root.right);

        return (si1+si2)+1;
    }



    /*
      获取叶子节点的个数：遍历思路
      */
    public  int leafSize = 0;

    void getLeafNodeCount1(TreeNode root) {

        if(root==null){
            return;
        }

        if(root.right==null && root.left==null){
           leafSize++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);

    }


    /*
     获取叶子节点的个数：子问题
     */
    int getLeafNodeCount2(TreeNode root) {
        if(root==null){
            return 0;
        }
        if(root.left==null&& root.left==null){
            return 1;
        }
        int si1=getLeafNodeCount2(root.left);
        int si2=getLeafNodeCount2(root.right);
        return si1+si2;
    }

    /*
  获取第K层节点的个数
   */
    int getKLevelNodeCount(TreeNode root, int k) {
        if(root==null){
            return 0;
        }
        if(k==1){//到第k层返回
            return 1;
        }

        int si1=getKLevelNodeCount(root.left,k-1);
        int si2=getKLevelNodeCount(root.right,k-1);

        return si1+si2;
    }
    /*
    获取二叉树的高度
    时间复杂度：O(N)
    */
    int getHeight(TreeNode root) {

        if(root==null){
            return 0;
        }
       int size1= getHeight(root.left);
        int size2=getHeight(root.right);
        int size3=Math.max(size1,size2);
        return size3+1;
    }
    // 检测值为value的元素是否存在
   public  TreeNode find(TreeNode root, char val) {

        if(root==null){
            return null;
        }

        if(root.val==val){
            return root;
        }
       TreeNode tree1= find(root.left,val);
        if(tree1!=null){
            return tree1;
        }
       TreeNode tree2= find(root.right,val);

        return tree2;
    }

    //层序遍历
    void levelOrder(TreeNode root) {

        if(root==null){
            return;
        }

        System.out.println(root.val);
        if(root.left!=null){
            System.out.println(root.left.val);
        }
        if(root.right!=null){
            System.out.println(root.right.val);
        }
        levelOrder(root.left);
        levelOrder(root.right);
    }

/*
    * 反转二叉树
    * https://leetcode.cn/problems/invert-binary-tree/
    * */

    public TreeNode invertTree(TreeNode root) {
        if(root==null){
            return null;

        }
        TreeNode node=root.left;
        root.left=root.right;
        root.right=node;
        invertTree(root.left);
        invertTree(root.right);

        return root;
    }
/*
https://leetcode.cn/problems/same-tree/description/
*100. 相同的树
* */

    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p!=null&&q==null||p==null&&q!=null){
            return false;
        }
        if(p==null&&q==null){
            return true;
        }
        if(p.val!=q.val){
            return false;
        }
        return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
    }

    /*
    https://leetcode.cn/problems/subtree-of-another-tree/description/
    * 另一棵树的子树
    * */
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {

        if(root==null){
            return false;
        }
        if(isSameTree(root,subRoot)){
            return true;
        }

        if(isSubtree(root.left,subRoot)||isSubtree(root.right,subRoot)){
            return true;
        }

        return false;

    }
    /*
  https://leetcode.cn/problems/balanced-binary-tree/
   平衡二叉树
    * */
    public boolean isBalanced(TreeNode root) {

        return isHighTree(root)>=0;
    }

    public int isHighTree(TreeNode root) {
        if (root == null) {
            return 0;
        }

        int size1 = isHighTree(root.left);
        int size2 = isHighTree(root.right);

        if(size1>=0&&size2>=0&&Math.abs(size1-size2)<2){
            return (size1 > size2 ? size1 : size2) + 1;
        }
        return -1;

    }

    /*
    * 、https://leetcode.cn/problems/symmetric-tree/
     对称二叉树*/
    public boolean isSymmetric(TreeNode root) {
        if(root==null){
            return false;
        }
        if(isSubertree(root.left,root.right)){
            return true;
        }

        return false;
    }

    public boolean isSubertree(TreeNode p,TreeNode q){
        if(p!=null&&q==null||p==null&&q!=null){
            return false;
        }
        if(p==null&&q==null){
            return true;
        }
        if(p.val!=q.val){
            return false;
        }

        return isSubertree(p.left,q.right)&&isSubertree(p.right,q.left);
    }

    // 判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root) {
        Queue<TreeNode> queue=new LinkedList<>();
        if(root==null){
            return true;
        }
        queue.offer(root);
        while(queue.peek()!=null){
            TreeNode cur=null;
            int size=queue.size();
            while(size!=0){
                size--;
                cur=queue.poll();
                if(cur!=null){
                    queue.offer(cur.left);
                    queue.offer(cur.right);
                }
            }
        }
        int i=queue.size();
        while(i!=0){
            i--;
            if(queue.poll()!=null){//如果队列里都为空证明是完全二叉树
                return false;
            }
        }

        return true;
    }
    //层序遍历
    public void levelOrder2(TreeNode root) {
        Queue<TreeNode> queue=new LinkedList<>();
        if(root==null){
            return;
        }
        queue.offer(root);
        while(!queue.isEmpty()){
            int size1=queue.size();
            TreeNode cur=null;
            while(size1!=0){
                cur=queue.poll();
                size1--;
                System.out.print(cur.val+" ");
                if(cur.left!=null){
                    queue.offer(cur.left);
                }
                if(cur.right!=null){
                    queue.offer(cur.right);
                }
            }

        }

    }
    /*
    *. 二叉树的层序遍历
    *https://leetcode.cn/problems/binary-tree-level-order-traversal/description/
    * */

    public List<List<Character>> levelOrder1(TreeNode root) {
        List<List<Character>> empList=new ArrayList<>();
        Queue<TreeNode> queue=new LinkedList<>();
            if(root==null){
                return empList;
            }
            queue.offer(root);
            while(!queue.isEmpty()){
                List<Character> list=new ArrayList<>();//每一层创建一个数组
                int si=queue.size();//判断队列中每一层有多少元素

                while(si!=0){//往队列中放元素
                    TreeNode cur=queue.poll();
                    list.add(cur.val);
                    si--;
                    if(cur.left!=null){
                        queue.offer(cur.left);
                    }
                    if(cur.right!=null){
                        queue.offer(cur.right);
                    }
                }
                empList.add(list);
            }
        return empList;
    }
    /*
    *https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/description/
    * 107. 二叉树的层序遍历 II*/
    public List<List<Character>> levelOrderBottom1(TreeNode root) {
        List<List<Character>> tmpList=new ArrayList<>();
        Queue<TreeNode> queue =new LinkedList<>();

        if(root==null){
            return tmpList;
        }
        queue.offer(root);
        while(!queue.isEmpty()){
            List<Character> list=new ArrayList<>();
            int size=queue.size();
            while(size!=0){
                TreeNode cur=queue.poll();
                list.add(cur.val);
                size--;

                if(cur.left!=null){
                    queue.offer(cur.left);
                }
                if(cur.right!=null){
                    queue.offer(cur.right);
                }

            }
            tmpList.add(list);
        }
        int i=0;
        int j=tmpList.size()-1;
        while(i<j){
            List<Character> tmp =tmpList.get(i);
            tmpList.set(i,tmpList.get(j));
            tmpList.set(j,tmp);
            i++;
            j--;
        }

        return  tmpList;

    }
    /*
    *236. 二叉树的最近公共祖先
    * https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/description/*/
    public TreeNode lowestCommonAncestor(TreeNode root,TreeNode p,TreeNode q){
        if(root==null){
            return  null;
        }
        if(root==p||q==root){
            return root;
        }
        TreeNode left=lowestCommonAncestor(root.left,p,q);
        TreeNode right=lowestCommonAncestor(root.right,p,q);
        if(left!=null&& right!=null){
            return root;
        }else if(left!=null){
            return left;
        }
        return right;
    }

    /*
    https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/
    * 从中序与后序遍历序列构造二叉树
    * public  int inorderindex;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        inorderindex=postorder.length-1;

        return buildTree1(inorder,postorder,0,inorder.length-1);

    }
    public TreeNode buildTree1( int[] inorder, int[] postorder,int begin,int inend) {
        if(begin>inend){
            return null;
        }
        TreeNode root=new TreeNode(postorder[inorderindex]);//创造根节点保存下来
        int findtreeindex=findindex(inorder,begin,inend,postorder[inorderindex]);//查找中序根节点下标
        inorderindex--;//后序根节点下标
        root.right=buildTree1(inorder,postorder,findtreeindex+1,inend);
        root.left=buildTree1(inorder,postorder,begin,findtreeindex-1);

        return root;
    }

       public int  findindex(int[] inorder,int begin,int inend,int key){//查找中间元素的下标

       for(int i=begin;i<=inend;i++){
        if(inorder[i]==key){
            return i;
        }
       }

        return -1;
       }*/

    /*

    https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/
    *从前序与中序遍历序列构造二叉树
    *  public  int preorderindex;
    public TreeNode buildTree(int[] preorder, int[] inorder) {

        return buildTree1(preorder,inorder,0,inorder.length-1);
    }
    public TreeNode buildTree1(int[] preorder, int[] inorder ,int inbegin,int inend) {

        if(inbegin>inend){
            return null;
        }
        //创建根节点
        TreeNode cur=new TreeNode(preorder[preorderindex]);
        //查找中间节点下标
        int inorderndex=findindex(inorder,preorder[preorderindex]);
        preorderindex++;

        cur.left=buildTree1(preorder,inorder,inbegin,inorderndex-1);
        cur.right=buildTree1(preorder,inorder,inorderndex+1,inend);

        return cur;
    }

    public int findindex(int[] inorder,int key){

        for(int i=0;i<inorder.length;i++){

            if(inorder[i]==key){
                return i;
            }
        }
        return -1;
    }*/

}
